<?php
/**
 *+------------------
 * Lflow
 *+------------------
 * Copyright (c) 2023~2030 gitee.com/liu_guan_qing All rights reserved.本版权不可删除，侵权必究
 *+------------------
 * Author: Mr.April(405784684@qq.com)
 *+------------------
 */
declare (strict_types=1);

namespace lflow\implement;

use lflow\Logger;
use lflow\WorkFlowInterceptor;
use lflow\workflow\Execution;

/**
 * 日志拦截器
 *
 * @author Mr.April
 * @since  1.0
 */
class LogInterceptor implements WorkFlowInterceptor
{

    /**
     * 拦截产生的任务对象，打印日志
     *
     * @param \lflow\workflow\Execution $execution
     */
    public function intercept(Execution $execution): void
    {
        if (Logger::isInfoEnabled()) {
            foreach ($execution->getTasks() as $task) {
                $buffer = "创建任务[标识=" . $task->getId();
                $buffer .= ",名称=" . $task->getDisplayName();
                $buffer .= ",创建时间=" . $task->getCreateTime();
                $buffer .= ",参与者={";
                if (!empty($task->getActorIds())) {
                    foreach ($task->getActorIds() as $actor) {
                        $buffer .= $actor . ';';
                    }
                    $buffer .= "}]";
                }
                Logger::info(json_encode($buffer));
            }
        }
    }
}